package com.geoboard;

import com.geoboard.Fragments.BrowseBoardFrag;
import com.geoboard.Fragments.PostsMapFrag;
import com.geoboard.Fragments.TabControlFragment;
import com.google.android.gms.maps.SupportMapFragment;
import com.locoboard.R;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.view.View;



public class MainActivity extends FragmentActivity 
			implements FragInterCommunication{
	
	
	private View contentContainer = null;
	private View tabContainer = null;
	
	private PostsMapFrag postMapFrag = null;	
	private BrowseBoardFrag boardMapFrag = null;
	private TabControlFragment tabFrag = null;
	
	private State curState = null;
	
	private SupportMapFragment mapFrag = null;
	
	/**** Methods ******************************************************/
		
	
	
	

	
	/**** Android Callback methods **************************************/
   
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);    
        
        getSupportFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
        
        mapFrag = new SupportMapFragment();
        
        if(contentContainer == null){
        	contentContainer = findViewById(R.id.fragment_container);   
        	tabContainer = findViewById(R.id.tab_container);
            
            postMapFrag = new PostsMapFrag();           
            tabFrag = new TabControlFragment();
            boardMapFrag = new BrowseBoardFrag();
            
            getSupportFragmentManager().beginTransaction().add(R.id.tab_container,tabFrag).commit();
            
            //getSupportFragmentManager().beginTransaction().add(R.id.fragment_container,postMapFrag).commit();
            changeState(State.POSTS_MAP);
            
            //setCurrentState(State.POSTS_MAP);
            
            
            tabContainer.bringToFront();
        }
        
        
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }    
	
	/**** FragInterCommunication Handlers *****************************/
    
    @Override
	public SupportMapFragment getMapFrag(){
		return mapFrag;
	}
    
    @Override
    public void setCurrentState(State currentState){
		curState = currentState;
		
		tabFrag.setSelectedTab(curState);
	}

	@Override
	public void changeState(State newState) {
		if(curState != newState){
						
			
			switch(newState){
				case BOARD_MAP:
					
					if(!getSupportFragmentManager().popBackStackImmediate("board", 0)){
						FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
						transaction.replace(R.id.fragment_container,boardMapFrag);
						transaction.addToBackStack("board");
						transaction.commit();
					}
					
					
					break;
				case POSTS_MAP:
				default:
					if(!getSupportFragmentManager().popBackStackImmediate("board", 0)){			
						FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
						transaction.replace(R.id.fragment_container,postMapFrag);
						transaction.addToBackStack("post");
						transaction.commit();
					}
						
					
			}			
		
			setCurrentState(newState);
		
			tabContainer.bringToFront();
		}
	}
    
}
